/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

package app.models.sys;

import app.Const;
import app.dtos.SmsMask;
import com.github.sog.annotation.TableBind;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.ehcache.CacheKit;

import java.util.List;

/**
 * <p>
 * .
 * </p>
 *
 * @author sagyf yang
 * @version 1.0 2014-05-21 14:38
 * @since JDK 1.6
 */
@TableBind(tableName = "ol_sms_category")
public class SmsCategory extends Model<SmsCategory> {
    /**
     * The public dao.
     */
    public static final SmsCategory dao = new SmsCategory();


    private static final long serialVersionUID = 6615127898441494970L;

    public void initCache() {
        List<SmsCategory> categories = find(SqlKit.sql("smscategory.list"));
        for (SmsCategory category : categories) {
            CacheKit.put(Const.CACHE_SYSTEM_CATEGORY, category.getStr("mask"), category);
        }
    }


    public SmsCategory findByMask(SmsMask mask) {
        return CacheKit.get(Const.CACHE_SYSTEM_CATEGORY, mask.toString());
    }

    public void insert(SmsMask mask, String name, String paramter, String template) {
        SmsCategory category = new SmsCategory();
        category.set("mask", mask.toString());
        category.set("name", name);
        category.set("parameter", paramter);
        category.set("template", template);
        category.save();
        CacheKit.put(Const.CACHE_IMPORT_MASK, mask.toString(), category);
    }

}